$(function(){
    // 菜单添加模块
    $("#add-menu").click(function(){
        $("input").val("");
        $('#edit').val('false');
        $('#parent').val('0');
        $.ajax({
            url: '/admin-menu-parent',
            type: 'get',
            success: function(data){
                let html = `<option value="0">一级菜单</option>`;
                for (let i = 0; i < data.length; i++) {
                    html += `<option value="`+data[i].id+`">`+data[i].menu_name+`</option>`;
                    for (let j = 0; j < data[i].second.length; j++) {
                        html += `<option value="`+data[i].second[j].id+`">&emsp;`+data[i].second[j].menu_name+`</option>`;
                    }
                }
                $('#parent').html(html);
                layer.open({
                    type: 1,
                    title: '添加菜单',
                    area: ['80vw', '80vh'],
                    content: $("#menu-form-content")
                });
            }
        })
    });
    $("#menu-submit").click(function(){
        let parent = $('#parent').val();
        let name = $('#name').val();
        let icon = $('#icon').val();
        let controller = $('#controller').val();
        let method = $('#method').val();
        let edit = $('#edit').val();
        let id = $('#id').val();
        if (edit == 'true') {
            $.ajax({
                url: '/admin-menu-edit',
                type: 'post',
                data: {id:id, menu_name:name, pid:parent, menu_icon:icon, controller:controller, method:method},
                success: function(data){
                    if(!data.status || data.code==0){
                        layer.msg(data.msg, {
                            icon: 5
                        });
                        return;
                    }
                    layer.msg(data.msg);
                    $('#menu').bootstrapTable('refresh', {url: '/admin-menu-infos'});
                }
            })
        }else{
            $.ajax({
                url: '/admin-menu-add',
                type: 'post',
                data: {menu_name:name, pid:parent, menu_icon:icon, controller:controller, method:method},
                success: function(data){
                    if(!data.status || data.code==0){
                        layer.msg(data.msg, {
                            icon: 5
                        });
                        return;
                    }
                    layer.msg(data.msg);
                    $('#menu').bootstrapTable('refresh', {url: '/admin-menu-infos'});
                }
            })
        }
    })

    // 菜单信息验证模块
    $('#menuForm').validate({
        rules: {
            name: {
                required: true,
                minlength: 2
            },
            parent: {
                required: true,
                digits: true
            }
        },
        messages: {
            name: {
                required: '请输入你的菜单名',
                minlength: '菜单名必须大于两位'
            },
            parent: {
                required: '请选择有效分类',
                digits: '请选择有效分类',
            }
        }
    });

    // 菜单信息列表模块
    window.operateEvents = {
        "click #editRow":function(e, value, row, index){
            $('#name').val(row.menu_name);
            $('#icon').val(row.menu_icon);
            $('#controller').val(row.controller);
            $('#method').val(row.method);
            $('#id').val(row.id);
            $('#edit').val('true');

            $.ajax({
                url: '/admin-menu-parent',
                type: 'get',
                success: function(data){
                    let html = '';
                    if (row.pid == 0) {
                        html += `<option value="0" selected>一级菜单</option>`;
                    }else{
                        html += `<option value="0">一级菜单</option>`;
                    }
                    for (let i = 0; i < data.length; i++) {
                        if (row.pid == data[i].id) {
                            html += `<option value="`+data[i].id+`" selected>`+data[i].menu_name+`</option>`;
                        }else{
                            html += `<option value="`+data[i].id+`">`+data[i].menu_name+`</option>`;
                        }
                        for (let j = 0; j < data[i].second.length; j++) {
                            if (row.pid == data[i].second[j].id) {
                                html += `<option value="`+data[i].second[j].id+`" selected>&emsp;`+data[i].second[j].menu_name+`</option>`;
                            }else{
                                html += `<option value="`+data[i].second[j].id+`">&emsp;`+data[i].second[j].menu_name+`</option>`;
                            }
                        }
                    }
                    $('#parent').html(html);
                    layer.open({
                        type: 1,
                        title: '<b>编辑菜单</b>--'+row.menu_name,
                        area: ['80vw', '80vh'],
                        content: $("#menu-form-content")
                    });
                }
            })
        },
        "click #deleteRow":function(e, value, row, index){
            $.ajax({
                url: '/admin-menu-delete',
                type: 'post',
                data: {id:row.id},
                success: function(data){
                    if(!data.status || data.code==0){
                        layer.msg(data.msg, {
                            icon: 5
                        });
                        return;
                    }
                    layer.msg(data.msg);
                    $('#menu').bootstrapTable('refresh', {url: '/admin-menu-infos'});
                }
            })
        }
    }
    $("#menu").bootstrapTable({
        url:"/admin-menu-infos",
        columns:[
            {
                field:"select",
                title:"全选",
                checkbox:true
            },
            {
                field:'id',
                title:'id',
                visible: false
            },
            {
                field:'menu_name',
                title:'菜单名'
            },
            {
                field:'pid',
                title:'父菜单id',
                visible: false
            },
            {
                field:'pid_title',
                title:'所属菜单'
            },
            {
                field:'menu_icon',
                title:'图标',
                visible: false
            },
            {
                field:'controller',
                title:'控制器',
                visible: false
            },
            {
                field:'method',
                title:'方法',
                visible: false
            },
            {
                field:'updater',
                title:'更新人'
            },
            {
                field:'update_time',
                title:'更新时间'
            },
            {
                field:'Button',
                title:'操作',
                events:operateEvents,
                formatter:addButtons,
            },
        ],
        striped:true,
        sortName:'id',                    // 排序字段名
        sortOrder:'asc',                   // 升降序，默认升序
        icons:{
            paginationSwitchDown: 'glyphicon-collapse-down icon-chevron-down',
            paginationSwitchUp: 'glyphicon-collapse-up icon-chevron-up',
            refresh: 'glyphicon-refresh icon-refresh',
            toggle: 'glyphicon-list-alt icon-list-alt',
            columns: 'glyphicon-th icon-th',
            detailOpen: 'glyphicon-plus icon-plus',
            detailClose: 'glyphicon-minus icon-minus',
        },
        pagination:true,                    // 分页
        onlyInfoPagination:false,           // 只显示数据量
        pageSize:10,                         // 每页数据量
        pageList:[10,20,30,50],                   // 可选择每页数据量
        search:false,                        // 显示搜索
        selectItemName:'id',
        showHeader:true,                    // 显示头
        showColumns:true,                   // 显示列下拉框
        showRefresh:true,                   // 显示刷新
        showToggle:true,                    // 切换试图
        showPaginationSwitch:true,          // 显示分页切换按钮
        idField:'id',                       // 指定主键
        uniqueId:'id',                      // 设置每一行唯一标识符
        detailView:false,                    // 显示详细页面
        searchAlign:'left',                // 搜索框位置
        buttonsAlign:'right',                 // 按钮位置
        paginationVAlign:'bottom',
        paginationHAlign:'right',
        paginationPreText:'上一页',
        paginationNextText:'下一页',
        toolbar:'#toolbar',
        toolbarAlign:'left',
        sidePagination:'server',            // 客户端还是服务端分页
        queryParams: function queryParams(params) {
            return {
                pageNumber: params.offset,
                pageSize: params.limit,
                name:$("#search_name").val()
            };
        },
    });
    $('#search_btn').click(function(){
        $('#menu').bootstrapTable('refresh', {url: '/admin-menu-infos'});
    });
    function addButtons(){
        return [
            "<button type='button' id='editRow' class='btn btn-success btn-xs'>编辑</button>&nbsp;&nbsp;",
            "<button type='button' id='deleteRow' class='btn btn-danger btn-xs'>删除</button>",
        ].join("")
    }
})
